package com.EE7th.controller;

import com.EE7th.Result.PageResult;
import com.EE7th.Result.Result;
import com.EE7th.domain.Clazz;
import com.EE7th.dto.PageQueryDTO;
import com.EE7th.service.ClazzService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CommonsLog
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //分页查询
    @GetMapping("/page")
    public Result<PageResult> page(PageQueryDTO pageQueryDTO){
        log.info("page query: " + pageQueryDTO);
        PageResult pageResult = clazzService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    // 添加课程
    @PostMapping("/add")
    public Result<String> saveClazz(@RequestBody Clazz clazz) {
        if (clazzService.insertClazz(clazz))
        return Result.success();
        else return Result.error("添加失败");
    }

    // 获取所有班级的id和名称
    @GetMapping("/select")
    public Result<List<Clazz>> getClazzSelection() {
        List<Clazz> selection = clazzService.getClazzSelection();
        return Result.success(selection);
    }
}